﻿using System;
using System.Windows.Forms;
using roster.Domain;

namespace roster.UI
{
    public partial class PlayerView : Form, IPlayerView
    {
        public PlayerView()
        {
            InitializeComponent();
            AddClassesToClassDropdown();
            
            txtName.Focus();
        }

        #region IPlayerView Members

        public string PlayerName { get; private set; }
        public Class Class { get; private set; }

        public void Display()
        {
            txtName.Text = string.Empty;
            if (ShowDialog() == DialogResult.OK)
            {
                AddPlayer();
            }
        }

        #endregion

        public event MethodInvoker AddPlayer;

        private void AddClassesToClassDropdown()
        {
            foreach (string @class in Enum.GetNames(typeof (Class)))
            {
                cboClasses.Items.Add(Enum.Parse(typeof (Class), @class));
            }
            cboClasses.SelectedIndex = 0;
        }

        private void SetState()
        {
            PlayerName = txtName.Text;
            Class = (Class) cboClasses.SelectedItem;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            CloseWithResponse(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseWithResponse(DialogResult.Cancel);
        }

        private void CloseWithResponse(DialogResult result)
        {
            DialogResult = result;
            SetState();
            Close();
        }

        private void cboClasses_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
    }
}